const Controller = require('egg').Controller;

class DictController extends Controller {
  // 新增字典
  async dict_add() {
    const query = this.ctx.request.body;
    const query_bol = this.ctx.helper.validInput(query, ['name', 'type']);
    if(query_bol != 'true') return query_bol;

    const params = {
      name: query.name,
      type: query.type,
      status: query.status,
      remark: query.remark
    };
    try {
      // 存入字典数据
      const res = await this.ctx.service.dict.dict_add(params);
      console.log('19...', res);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: {}
      };
    }catch(err) {
      return this.ctx.body = {
        s: 0,
        m: 'fail~',
        d: {}
      }
    }
  }
  // 更新字典
  async dict_update() {
    const query = this.ctx.request.body;
    const query_bol = this.ctx.helper.validInput(query, ['id', 'name', 'type']);
    if(query_bol != 'true') return query_bol;

    const params = {
      name: query.name,
      value: query.value,
      status: query.status,
      remark: query.remark
    };
    try {
      // 存入字典数据
      const res = await this.ctx.service.dict.dict_update(query.id, params);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: {}
      };
    }catch(err) {
      return this.ctx.body = {
        s: 0,
        m: 'fail~',
        d: {}
      }
    }
  }
  // 字典列表
  async dict_list() {
    const query = this.ctx.request.query;
    console.log('66...', this.ctx.request.query);
    try {
      const dictRes = await this.ctx.service.dict.dict_list(query);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: dictRes
      };
    }catch(err) {
      return this.ctx.body = {
        s: 0,
        m: `fail~${err}`,
        d: {}
      };
    }
  }
  // 删除字典
  async dict_del() {
    const query = this.ctx.request.body;
    const query_bol = this.ctx.helper.validInput(query, ['id']);
    if(query_bol != 'true') return query_bol;

    try {
      await this.ctx.service.dict.dict_del(query.id);
      this.ctx.status = 200;
      return this.ctx.body = {
        s: 1,
        m: 'ok~',
        d: {}
      };
    }catch(err) {
      return this.ctx.body = {
        s: 0,
        m: `fail~${err}`,
        d: {}
      };
    }
  }
}

module.exports = DictController;